<?php

/*
 * Action 5$
 *
 * Choose one: Gain a card from the trash costing from 3 Coin to 6 Coin, putting it on top of your deck; or trash an Action card from your hand and gain a card costing up to 3 Coin more than it.
 */

class Graverobber extends Card implements iAction, iKingdom, iDarkAges {

    public function __construct() {
        parent::__construct("Graverobber", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "Choose one: Gain a card from the trash costing from 3 Coin to 6 Coin, putting it on top of your deck; or trash an Action card from your hand and gain a card costing up to 3 Coin more than it.");
    }

    public function play(Game $game, $isRealCard) {
        $options = array(new Option(1, "Gain a card from trash"), new Option(2, "Trash an action from your hand"));
        $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Please choose");
        $answer = $output[0];
        if ($answer == "Gain a card from trash") {
            $trashCards = $game->getTrashCardsByPrice(new Price(3), new Price(6));
            $trashCards = getUniqueCards($trashCards);
            if (sizeof($trashCards) == 0) {
                ScreenDisplay::addScreenMessage($game, true, "gain nothing", "gains nothing");
            } elseif (sizeof($trashCards) == 1) {
                $card = $game->moveCardFromPileToPile($trashCards[0], Trash, DrawPile);
                ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($card), "gains " . ScreenDisplay::$LMSG);
            } else {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $trashCards, 1, 1, null, "Please choose");
                $card = $game->moveCardFromPileToPile($output[0], Trash, DrawPile);
                ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($card), "gains " . ScreenDisplay::$LMSG);
            }
        } else {

            $playHand = $game->getPlayHand();
            $actions = array();
            foreach ($playHand as $card) {
                if ($card instanceof iAction) {
                    $actions[] = $card;
                }
            }

            if (sizeof($actions) > 0) {
                $actions = getUniqueCards($actions);
                if (sizeof($actions) > 1) {
                    $output = MySocket::GetUserReply($game, Input_ChooseOne, $actions, 1, 1, null, "Choose card to trash");
                    $card_name = $output[0];
                }
                else {
                    $card_name = $actions[0];
                }
                
                $old_card = $game->moveCardFromPileToPile($card_name, PlayHand, Trash);
                ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($old_card), "trashes " . ScreenDisplay::$LMSG);

                $price = $old_card->getPrice($game);
                $new_price = $price->add(3);
                $optional_cards = $game->getBoardCardsByPrice(new Price(0), $new_price);

                $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards, 1, 1, null, "Choose a card to gain");
                $card_name = $output[0];
                $new_card = $game->moveCardFromPileToPile($card_name, BoardCards, DiscardPile);
                ScreenDisplay::addScreenMessage($game, false, " and gain " . nicePrint($new_card), " and gains " . ScreenDisplay::$LMSG);
            } else {
                ScreenDisplay::addScreenMessage($game, true, "trash nothing", "trashes nothing");
            }
        }
        $game->cardActionFinished();
        return true;
    }

}

?>